﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using XT.Exam.BLL;
using XT.Exam.Common;
using System.Data.SqlClient;
using XT.Exam.MODEL;
using XT.Exam.IBLL;
using XT.Exam.IDataAccess;


namespace ExamSystemMvc.Controllers
{
    public class AdminController : Controller
    {
        //
        // GET: /Admin/


        public ActionResult Index()
        {
            return View();
        }
        
        public ActionResult Code()
        {
            string code = string.Empty;
            byte[] bytes =ValidateCode.CreateValidateGraphic(out code, 4, 100, 40, 20);
            SessionHelper.SetSession("code",code);
            return File(bytes, @"image/jpeg");
        }
        public ActionResult Login()
        {
            return View();
        }
        [HttpPost]
        [HandleError(View="//")]
        public ActionResult LoginDo(string uName, string uPass, string Validate, string role)
        {
            var CheckCode = SessionHelper.GetSessionStr("code");
            if (Validate.ToUpper() != CheckCode.ToString())
            {
                return Content("<script>alert('验证码错误，请重新输入;');location.href='/Admin/Login'</script>");
            }
            else
            {
                uPass = MD5DBhelper.MD5Encrypt32(uPass);
                SysUserBLL students = new SysUserBLL();
                SysUserInfoBLL sysuserinfo = new SysUserInfoBLL();
                SysLecturerBLL lecturer = new SysLecturerBLL();
                SqlParameter[] parameter =
                 {
                        new SqlParameter("@Student_Numb",uName),
                        new SqlParameter("@Student_Pass",uPass),
                        new SqlParameter("@RoleID",role),
                 };
                SqlParameter[] parameter2 =
                 {
                        new SqlParameter("@Lecturer_Numb",uName),
                        new SqlParameter("@Lecturer_Pass",uPass),
                        new SqlParameter("@RoleID",role),
                 };
                SqlParameter[] parameter3 =
                 {
                        new SqlParameter("@UserInfo_Nnumb",uName),
                        new SqlParameter("@UserInfo_Pass",uPass),
                        new SqlParameter("@RoleID",role),
                 };
                var student = students.GetList<Student>(parameter).FirstOrDefault();
                var lecturers = lecturer.GetList<Lecturer>(parameter2).FirstOrDefault();
                var userinfo = sysuserinfo.GetList<SystemUserInfo>(parameter3).FirstOrDefault();
                Session["student"] = student;
                Session["lecturer"] = lecturers;
                Session["userinfo"] = userinfo;
                if (lecturers != null)
                {
                    if (lecturers.ID > 0)
                    {
                        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(uName, false, 1);
                        //加密票据
                        string ticString = FormsAuthentication.Encrypt(ticket);
                        //输出到客户端
                        FormsAuthentication.SetAuthCookie(ticString, false);
                        return Redirect(Url.Action("Welcome", "Lecturer"));
                    }
                    else
                    {
                        return Content("<script>alert('登陆名或密码错误');location.href='/Admin/Login'</script>"); 
                    }
                }
                if (student != null)
                {
                    if (student.ID > 0)
                    {
                        //创造票据
                        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(uName, false, 1);
                        //加密票据
                        string ticString = FormsAuthentication.Encrypt(ticket);
                        //输出到客户端
                        FormsAuthentication.SetAuthCookie(ticString, false);
                        return Redirect(Url.Action("Index", "Student"));
                    }
                    else
                    {
                        return Content("<script>alert('登陆名或密码错误');location.href='/Admin/Login'</script>"); 
                    }
                }
                if (userinfo!=null)
                {
                    if (userinfo.ID > 0)
                    {
                        //创造票据
                        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(uName, false, 1);
                        //加密票据
                        string ticString = FormsAuthentication.Encrypt(ticket);
                        //输出到客户端
                        FormsAuthentication.SetAuthCookie(ticString, false);
                        return Redirect(Url.Action("Index", "SysUserInfo"));
                    }
                    else
                    {
                        return Content("<script>alert('登陆名或密码错误');location.href='/Admin/Login'</script>"); 
                    }
                }
                return Content("<script>alert('登陆名或密码错误');location.href='/Admin/Login'</script>"); 
            }
        }
        /// <summary>
        /// 错误页面
        /// </summary>
        /// <returns></returns>
        public ActionResult ErrView()
        {
            return View();
        }
    }
}
